package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {

    @Autowired
    UserService userService;


    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody @Validated UserLoginParam param){
        log.debug("用户登录信息："+param);
        UserVO userVO=userService.login(param);
        log.debug("用户登录成功："+userVO);
        return JsonResult.ok(userVO);

    }

    @GetMapping("select")
    @Operation(summary = "查询用户")
    @ApiOperationSupport(order = 20)
    public JsonResult selectUser(UserQuery query){
        log.debug("查询用户信息："+query);
        List<UserVO> list=userService.selectUser(query);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    @Operation(summary = "新增用户")
    @ApiOperationSupport(order = 30)
    public JsonResult addUser(@Validated UserSaveParam param){
        log.debug("新增用户信息："+param);
       userService.save( param);
       return JsonResult.ok();
    }

    @PostMapping("update/{userId}/{status}")
    @Operation(summary = "修改用户状态")
    @ApiOperationSupport(order = 40)
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status){
        log.debug("修改用户状态："+userId+"--"+status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 50)
    public JsonResult deleteUser(Long id){
        log.debug("删除用户："+id);
        userService.deleteUserId(id);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    @Operation(summary = "查询审核人列表")
    @ApiOperationSupport(order = 60)
    public JsonResult selectAuidit(@PathVariable Long parentId){
        log.debug("查询审核人列表："+parentId);
        List<UserVO> list = userService.selectAuditList(parentId);
        return JsonResult.ok(list);
    }

    @PostMapping("update/{userId}")
    @Operation(summary = "修改用户密码")
    @ApiOperationSupport(order = 70)
    public JsonResult updatePassword(@PathVariable Long userId){
        log.debug("修改用户密码："+userId);
        userService.updatePassword(userId);
        return JsonResult.ok();
    }






}
